#include "gsmeg_link_manager.h"

static z_buffer_t *create_gsmeg_req_with_gsmeg_hash(z_hash_t *gsmeg_hash){    
    int rsp_code = 0;
    z_buffer_t *gsmeg_json = gtp2gsmeg_hash2json(gsmeg_hash,&rsp_code);
    if(gsmeg_json != NULL){
        return gsmeg_json;
    }else{
        Z_LOG_ERROR("hash 2 json error[%d]",rsp_code);
        return NULL; 
    }
}

static void send_gsmeg_login_req(gsmeg_link_map_t *link_map,int channel,char *dev_name,long long dev_id){
    z_hash_t *gsmeg_hash = zH_init(); 
    if(gsmeg_hash == NULL){
        Z_LOG_FATAL("init hash fail");
        return; 
    }
    zH_strinsert_str(gsmeg_hash,"app_group_name",dev_name);
    zH_strinsert_ll(gsmeg_hash,"app_id",dev_id); 
    z_buffer_t *gsmeg_json = create_gsmeg_req_with_gsmeg_hash(gsmeg_hash);
    if(gsmeg_json == NULL){
        zH_free(gsmeg_hash);
        Z_LOG_ERROR("create gsmeg login req fail");
        return; 
    }
    gsmeg_link_map_push_by_channel(link_map,channel,(char *)(gsmeg_json->ptr_),gsmeg_json->used_,cmd_gsmeg_login_req);
    zH_free(gsmeg_hash);
    zB_free(gsmeg_json);
    return;
}    

void cb_on_gsmeg_link_connected(gsmeg_link_map_t *link_map,int channel,char *dev_name,long long dev_id){
    gsmeg_link_map_onconnected(link_map,channel);
    send_gsmeg_login_req(link_map,channel,dev_name,dev_id);
}

void cb_on_gsmeg_link_disconnected(z_hash_t* link_map, int channel){
    gsmeg_link_map_ondisconnected(link_map,channel);
}

int gsmeg_login_rsp_handler(z_hash_t *link_map,z_hash_t *gsmeg_hash,int channel){
    long long rsp_code = 0;
    if(NULL != zH_strfind_ll(gsmeg_hash,"result",&rsp_code)){
        if(rsp_code == 0){
            Z_LOG_NOTICE("gmseg channel[%l] logined successfully",channel);
            gsmeg_link_map_onlogined(link_map,channel);
            return 0;
        }else{
            Z_LOG_ERROR("gmseg channel[%l] login fail,result code[%d]",channel,rsp_code);
            return -1;
        }
    }else{
        Z_LOG_FATAL("result missed in gsmeg login rsp channel[%l]",channel);
        return -1;
    }
}    

int gsmeg_repair_rsp_handler(z_hash_t *link_map,z_hash_t *gsmeg_hash,int channel){
    long long rsp_code = 0;
    if(NULL != zH_strfind_ll(gsmeg_hash,"result",&rsp_code)){
        if(rsp_code == 0){
            Z_LOG_NOTICE("repair success for channel[%l]",channel);
            return 0;
        }else{
            Z_LOG_ERROR("repair fail for channel[%l],result code[%d]",channel,rsp_code);
            return -1;
        }
    }else{
        Z_LOG_FATAL("result missed in gsmeg repair rsp channel[%l]",channel);
        return -1;
    }
}    

void send_gsmeg_repair_req(z_hash_t *gsmeg_link_hash,int flow_type,long long seq_no){
    z_hash_t *gsmeg_hash = zH_init(); 
    if(gsmeg_hash == NULL){
        Z_LOG_FATAL("init hash fail");
        return; 
    }
    zH_strinsert_ll(gsmeg_hash,"start_seq",seq_no+1);
    zH_strinsert_ll(gsmeg_hash,"end_seq",0); 
    z_buffer_t *gsmeg_json = create_gsmeg_req_with_gsmeg_hash(gsmeg_hash);
    if(gsmeg_json == NULL){
        zH_free(gsmeg_hash);
        Z_LOG_ERROR("create gsmeg_req fail");
        return; 
    }
    gsmeg_link_map_push_to_one_valid_link(gsmeg_link_hash,(char *)(gsmeg_json->ptr_),gsmeg_json->used_,cmd_gsmeg_repair_req);
    zB_free(gsmeg_json);
    zH_free(gsmeg_hash);
    return;
}


